<template xmlns:el-col="http://www.w3.org/1999/html">
<div>
  <!--  面包屑导航区域 -->
  <br>
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item >首页</el-breadcrumb-item>
    <el-breadcrumb-item>个人中心</el-breadcrumb-item>
  </el-breadcrumb>
  <el-row class="home" :gutter="20" :data="storageItem">
    <el-col :span="12" :offset="6" style="margin-top: 20px">
      <el-card shadow="always">
        <div class="user">
          <img :src="userImg" />
          <el-row :gutter="20">
            <el-col :span="6" :offset="3">
              <div class="userInfo">
                <p class="id">Hello!{{storageItem.roleIDTip}}</p>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="8" :offset="3">
              <div class="userInfo">
                <p class="access">ID:{{ storageItem.userID }}</p>
                <p class="name">Name:{{ storageItem.username }}</p>
              </div>
            </el-col>
            <el-col :span="8" :offset="3" >
              <p class="date">HireDate:{{ storageItem.hireDate }}</p>
              <p class="tel">Tel:{{ storageItem.tel }}</p>
<!--              <p class="isonjob">OnJob:{{ storageItem.isOnJobTip }}</p>-->
            </el-col>
          </el-row>

          <el-row :gutter="20">
            <el-col :span="6" :offset="9">
              <el-button type="info" @click="editDialogVisible=true">修改个人信息</el-button>
            </el-col>
          </el-row>
        </div>
        <div class="login -edit"></div>
      </el-card>
    </el-col>
  </el-row>

  <!--  修改个人信息的对话框-->
  <el-dialog title="修改个人信息"
             :visible.sync="editDialogVisible" width="50%">
    <!--    内容主题区域-->
    <el-form :model="storageItem" label-width="120px">
      <el-form-item label="用户账号" prop="userID">
        <el-input v-model="storageItem.userID" disabled></el-input>
      </el-form-item>
      <el-row :gutter="1">
        <el-col :span="7.5">
          <el-form-item label="Name修改" prop="usernameTip">
            <el-switch v-model="editFlag.usernameTip"
                       style="display: block;padding-top: 10px"
                       active-color="#13ce66"
                       inactive-color="#ff4949"
                       active-text="YES"
                       inactive-text="NO">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="15">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="storageItem.username" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="1">
        <el-col :span="7.5">
          <el-form-item label="密码修改" prop="passwordTip">
            <el-switch v-model="editFlag.passwordTip"
                       style="display: block;padding-top: 10px"
                       active-color="#13ce66"
                       inactive-color="#ff4949"
                       active-text="YES"
                       inactive-text="NO">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="15">
          <el-form-item label="密码" prop="password">
            <el-input v-model="storageItem.password" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="1">
        <el-col :span="7.5">
          <el-form-item label="电话修改" prop="telTip">
            <el-switch v-model="editFlag.telTip"
                       style="display: block;padding-top: 10px"
                       active-color="#13ce66"
                       inactive-color="#ff4949"
                       active-text="YES"
                       inactive-text="NO">
            </el-switch>
          </el-form-item>
        </el-col>
        <el-col :span="15">
          <el-form-item label="电话" prop="tel">
            <el-input v-model="storageItem.tel" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <!--    底部区域-->
    <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogClosed">取 消</el-button>
        <el-button type="primary" @click="editSupplierInfo">确 定</el-button>
      </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: 'PersonalMGT',
  data(){
    return{
      userImg:require("../../assets/userImg.png"),
      storageItem:{
      },
      //控制修改页面
      editDialogVisible:false,
      //控制修改标记flag
      editFlag:{
        usernameTip:true,
        passwordTip:true,
        telTip:true
      }
    }
  },
  created () {
    this.getStorageItem()
  },
  methods:{
    //查询用户信息
    async getStorageItem(){
      const { data: res } =
        await this.$http.post('/showLoginUserInfo', [])
      if (res.code == 10401) {
        await this.$router.push('/login')
        window.sessionStorage.clear()
        return this.$message.error('您没有权限，请重新登陆！')
      }
      if (res.code == 10200) {
        this.storageItem = res.data
        this.editFlag.usernameTip=true;
        this.editFlag.passwordTip=true;
        this.editFlag.telTip=true;
        if(res.data.roleID==100)
          this.storageItem.roleIDTip='Boss'
        if(res.data.roleID==111)
          this.storageItem.roleIDTip='User'
        // if(res.data.isOnJob)
        //   this.storageItem.isOnJobTip='在职'
        // if(!res.data.isOnJob)
        //   this.storageItem.isOnJobTip='已离职'
        console.log(this.storageItem)
      }
    },
    //监听修改对话框的关闭事件
    editDialogClosed(){
      this.editDialogVisible=false
      this.getStorageItem()
    },
    //修改用户信息并提交
    async editSupplierInfo(){
      let item={userID:this.storageItem.userID}
      if(this.editFlag.usernameTip==true)
        item.username=this.storageItem.username
      if(this.editFlag.passwordTip==true)
        item.password=this.storageItem.password
      if(this.editFlag.telTip==true)
        item.tel=this.storageItem.tel
      const { data: res } =
        await this.$http.post('/editPersonalInfo', item)
      if (res.code == 10401) {
        await this.$router.push('/login')
        window.sessionStorage.clear()
        return this.$message.error('您没有权限，请重新登陆！')
      }
      if (res.code == 10404)
        return this.$message.error('查询用户失败！')
      if (res.code == 10200) {
        //隐藏修改用户信息的对话框
        this.editDialogClosed()
        this.$message.success('修改成功！')
        //重新获取用户列表数据
        await this.getStorageItem()
        if(this.editFlag.passwordTip==true){
          await this.$router.push('/login')
          window.sessionStorage.clear()
          return this.$message.error('密码已修改，请重新登陆！')
        }
      }
    }
  }
}
</script>

<style scoped>
img{
  width: 250px;
  height: 250px;
  margin-left: 150px;
  border-radius: 50%;
}
userInfo{
  display: flex;
  align-items: center;
  padding-bottom: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid #ccc;
}
.id{
  font-size: 32px;
  margin-bottom: 10px;
}
.access,.name,.tel,.date{
  color: #999999;
}
</style>
